using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.Data;
using System.Data.OleDb;
using QLNS.Data;
using QLNS.Object;
using System.Windows.Forms;

namespace QLNS.Control
{
    public class NguoiDung_ctrl
    {
        NguoiDung_db data = new NguoiDung_db();

        public NguoiDung_ctrl()
        {
        }
        
        public int CheckLogin(string username, string password)
        {
            bool state =false;         
            DataTable nd = data.LayNguoiDung();
            int count = 0;
            for (int i = 0; i < nd.Rows.Count;i++ )
            {
                if (nd.Rows[i][2].ToString() == username)
                {
                    state = true; count = i;
                }
            }
            if (state)
            {
                if (nd.Rows[count][3].ToString() != password)
                    return 1;
                else
                {
                    NguoiDung_obj nd_info = new NguoiDung_obj(nd.Rows[count][0].ToString(),nd.Rows[count][2].ToString(),
                        nd.Rows[count][3].ToString(), Convert.ToInt32(nd.Rows[count][5]));

                    staticClass.NguoiDungStatic = nd_info;
                    return 2;
                }
            }
            return 0;
        }
        public int DangNhap(string username, string password)
        {
            DataTable nd =  data.LayNguoiDung();
            if (nd.Rows[0][1].ToString()!=username)
                return 0;//Khong ton tai nguoi dung nay

            string matkhau_hethong = nd.Rows[0][2].ToString();//MNat khau trong CSDL

            if (matkhau_hethong != password)
            {
                return  1;//Sai mat khau
            }
            else
            {
                NguoiDung_obj ndinfo=new NguoiDung_obj(nd.Rows[0][0].ToString(),nd.Rows[0][2].ToString(),nd.Rows[0][3].ToString(),Convert.ToInt32(nd.Rows[0][5]));

                staticClass.NguoiDungStatic = ndinfo;
                return 2;//Dang nhap thanh cong
            }
        }
        public void HienThiDataGridView(DataGridView view, BindingNavigator binding)
        {
            BindingSource bs = new BindingSource();
            DataTable dt = data.DanhSachNguoiDung();                   

            bs.DataSource = dt;
            binding.BindingSource = bs;
            view.DataSource = bs;
        }
        public void TimTenDangNhap(string tendangnhap)
        {
            data.TimTenDangNhap(tendangnhap);
        }
        public void TimMaNguoiDung(string ma)
        {
            data.TimMaNguoiDung(ma);
        }
        public DataRow NewRow()
        {
            return data.NewRow();
        }
        public void Add(DataRow row)
        {
            data.Add(row);
        }
        public bool Save()
        {
            return data.Save();
        }
    }
}
